package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Interface.user.service.RolePermissionService;
import com.example.user.dao.mapper.PermissionMapper;
import com.example.user.dao.mapper.RoleMapper;
import com.example.user.dao.mapper.RolePermissionMapper;
import com.example.user.dao.mapper.UserRoleMapper;
import com.example.user.dao.po.RolePermission;
import com.example.user.dao.po.UserRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
@RequiredArgsConstructor
@Slf4j
public class RolePermissionServiceImpl implements RolePermissionService {

    private final RolePermissionMapper rolePermissionMapper;
    private final PermissionMapper permissionMapper;
    private final RoleMapper roleMapper;

    private final UserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public boolean deletePermission(Long permissionId) {
        log.info("deletePermission rpc 服务被调用");
        if(rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getPermId, permissionId)) <= 0){
            return false;
        }
        return permissionMapper.deleteById(permissionId) > 0;
    }

    @Override
    @Transactional
    public boolean assignPermissionsToRole(Long roleId, List<Long> permissionIds) {
        log.info("assignPermissionsToRole rpc 服务被调用");
        if (rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRoleId, roleId)) <= 0){
            return false;
        }

        List<RolePermission> relations = permissionIds.stream()
                .map(permId -> RolePermission.builder()
                        .roleId(roleId)
                        .permId(permId)
                        .build())
                .collect(Collectors.toList());
        for (RolePermission relation : relations) {
            if (rolePermissionMapper.insert(relation) <= 0){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<String> getPermissionsByRoleId(Long roleId) {
        log.info("getPermissionsByRoleId rpc 服务被调用");
        return permissionMapper.selectPermKeysByRoleId(roleId);
    }

    @Override
    public List<String> getPermissionsByUserId(Long userId) {
        log.info("getPermissionsByUserId rpc 服务被调用");
        return roleMapper.selectPermIdsByUserId(userId);
    }

    @Override
    public List<String> getRolesByUserId(Long userId){
        log.info("getRolesByUserId rpc 服务被调用");
        return roleMapper.selectRoleKeysByUserId(userId);
    }

    @Override
    @Transactional
    public boolean assignRolesToUser(Long userId, List<Long> roleIds) {
        log.info("assignRolesToUser rpc 服务被调用");
        // 删除旧角色
        userRoleMapper.delete(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId, userId));

        // 插入新角色
        List<UserRole> relations = roleIds.stream()
                .map(roleId -> UserRole.builder()
                        .userId(userId)
                        .roleId(roleId)
                        .build())
                .collect(Collectors.toList());

        for (UserRole relation : relations) {
            if (userRoleMapper.insert(relation) <= 0){
                return false;
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean deleteRole(Long roleId) {
        log.info("deleteRole rpc 服务被调用");
        // 删除角色关联的用户和权限
        userRoleMapper.delete(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getRoleId, roleId));
        rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRoleId, roleId));
        return userRoleMapper.deleteById(roleId) > 0;
    }
}